package cn.lcfms.test;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.lcfms.app.admin.bean.DemoBean;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.utils.Vardump;

@RunWith(SpringJUnit4ClassRunner.class)    
@ContextConfiguration(locations = {"classpath*:springmvc-context.xml"}) 
public class T3{
	
	public void t1(){		
		BaseService service = Application.getService("demo");			
		service.setData("abc",1,'a').insert("s","i","t");
		//最近一次插入操作的自增id
		int insert_id = service.insert_id();
		System.out.println(insert_id);
	}
	
	public void t2(){		
		BaseService service = Application.getService("demo");	
		HashMap<String, Object> map=new HashMap<>();
		map.put("t", 's');
		map.put("i", 1);
		map.put("s", "abcde");
		service.setData(map).insert("s","i","t");
		//最近一次插入操作的自增id
		int insert_id = service.insert_id();
		System.out.println(insert_id);
	}
	
	public void t3(){		
		BaseService service = Application.getService("demo");	
		DemoBean demo=new DemoBean();
		demo.setI(5);	
		demo.setS("aaa");
		demo.setT("n");
		//这里s跟t都不会被插入
		service.insert(demo,"s,t");
	}
	

	/* 执行删除操作
	 * sql:DELETE FROM `demo` WHERE (`id`=?) 
	 * param:1(Integer)
	 */
	public void t4(){		
		BaseService service = Application.getService("demo");		
		//where("id=1")也可以,但如果1是变量的话不建议使用字符串拼接,不安全容易被注入
		service.where("`id`=#{id}").setData(1).delete();
		//最近一次sql操作影响的行数
		int affected_rows = service.affected_rows();
		System.out.println(affected_rows);
	}
	
	public void t5(){		
		BaseService service = Application.getService("demo");	
		//直接传主键id的值,程序会自动获取主键名并删除行
		service.deleteById(5,'f');
		//最近一次sql操作影响的行数
		int affected_rows = service.affected_rows();
		System.out.println(affected_rows);
		
	}
	
	public void t6(){		
		BaseService service = Application.getService("demo");	
		service.where("id=#{id}").setData("abc",123,'m',15).update("s","i","t");
	}
	
	public void t7(){		
		BaseService service = Application.getService("demo");	
		DemoBean demo=new DemoBean();		
		demo.setI(5);	
		demo.setS("aaa");
		demo.setId(13);
		service.update(demo,"i");			
	}
	
	public void ts() {
		BaseService service = Application.getService("demo");
		service.column("sum(i) as sum","s","t");
		service.groupBy("s","t");
		service.having("sum>10","sum<100");
		List<HashMap<String, Object>> list = service.selectList();
		Vardump.print(list); 
	}
	
	
	public void t8(){	
		BaseService service = Application.getService("demo");
		//查询一张表,返回List结果集
		List<HashMap<String, Object>> list = service.selectList();

		Vardump.print(list);
		//查询一张表满足条件的第一行的某一个字段,返回该字段的值
		int i=service.selectColumn("i"); 
		
		System.out.println(i);
		//查询满足条件的行数,返回行数int类型
		int count=service.selectCount();
		System.out.println(count);
		//查询一个javabean,返回该javabean对象
		service.where("id",5);
		service.where("t", "6");
		DemoBean bean=service.selectOne(DemoBean.class);
		Vardump.print(bean);
		//查询满足条件的第一个map,返回hashmap
		HashMap<String, Object> map = service.selectMap();
		Vardump.print(map);
		//查询一个分页数据,返回List结果集,其中字段count表示满足该分页的总行数
		List<HashMap<String, Object>> pagedata = service.selectPage(15, 1);	
		Vardump.print(pagedata);
	}
	

	
	public void t9() {
		BaseService service = Application.getService(DemoBean.class);	
		DemoBean demo=new DemoBean();
		demo.setI(5);	
		demo.setS("aaa");
		demo.setT("n");
		service.insert(demo,"t");
		//自增的主键id将自动映射到实体属性中
		Vardump.print(service.getSqlBuild());
	}
	
	@Test
	public void t11() {
		
	}

	public static void main(String[] args){
		File file=new File("https://www.baidu.com/");
		Vardump.print(file.canRead());
	}
}
